﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RPGX.Data;
using RPGXEngine.Server.ActionMessages;
using RPGXEngine.Server;
using RPGX.Server.Exceptions;

namespace RPGX.Server
{
    public class ActionMessageProcessor
    {
        #region Properties

        public Server Server
        {
            get { return _server; }
            private set { _server = value; }
        }
        private Server _server = null;

        #endregion

        #region Public Methods

        public ActionMessageProcessor(Server server)
        {
            this.Server = server;
        }

        public int Process()
        {
            int numProcessed = 0;

            var messages = ActionMessageRepository.GetActionMessages();

            foreach (var message in messages)
            {
                if (this.ProcessMessage(message))
                    numProcessed++;
            }

            return numProcessed;
        }

        public bool CheckForSystemResume()
        {
            var m = ActionMessageRepository.GetSystemResumeMessage();
            if (m != null)
            {
                this.Server.Resume();
                ActionMessageRepository.MarkMessageAsActioned(m);
            }

            return true;
        }

        #endregion

        #region Private Methods

        private bool ProcessMessage(IServerActionMessage message)
        {
            bool res = false;

            switch (message.Message)
            {
                case ServerActionMessages.Stop:
                    {
                        res = this.ActionMessage_Stop(message);
                        break;
                    }
                case ServerActionMessages.Restart:
                    {
                        res = this.ActionMessage_Restart(message);
                        break;
                    }
                case ServerActionMessages.Pause:
                    {
                        res = this.ActionMessage_Pause(message);
                        break;
                    }
                case ServerActionMessages.Resume:
                    {
                        res = this.ActionMessage_Resume(message);
                        break;
                    }
                default:
                    {
                        throw new InvalidServerActionMessageException((ServerActionMessages)message.Message);
                    }
            }

            if (res)
            {
                ActionMessageRepository.MarkMessageAsActioned(message);
            }

            return res;
        }

        #region Action Methods

        private bool ActionMessage_Stop(IServerActionMessage m)
        {
            return this.Server.Stop();
        }

        private bool ActionMessage_Restart(IServerActionMessage m)
        {
            return this.Server.Restart();
        }

        private bool ActionMessage_Pause(IServerActionMessage m)
        {
            return this.Server.Pause();
        }

        private bool ActionMessage_Resume(IServerActionMessage m)
        {
            return this.Server.Resume();
        }

        #endregion

        #endregion

        #region Static Methods

        public static IServerActionMessage FromDBMessage(ServerActionMessage message)
        {
            IServerActionMessage m = null;

            switch ((ServerActionMessages)message.Message)
            {
                case ServerActionMessages.Stop:
                    {
                        m = new ServerActionMessage_Stop();
                        break;
                    }
                case ServerActionMessages.Restart:
                    {
                        m = new ServerActionMessage_Restart();
                        break;
                    }
                case ServerActionMessages.Pause:
                    {
                        m = new ServerActionMessage_Pause();
                        break;
                    }
                case ServerActionMessages.Resume:
                    {
                        m = new ServerActionMessage_Resume();
                        break;
                    }
                default:
                    {
                        throw new InvalidServerActionMessageException((ServerActionMessages)message.Message);
                    }
            }

            m.ActionMessageId = message.ServerActionMessageId;

            return m;
        }

        #endregion
    }
}
